package com.turbo.boot.crawler.config;

import com.turbo.starter.mybatis.TurboDataSourceAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/29
 */
@Slf4j
@Configuration
//@MapperScan("com.turbo.starter.adapter.mapper")
@MapperScan(basePackages = {"com.turbo.boot.crawler.dao.mapper"}, sqlSessionFactoryRef = "turboBootCrawlerSqlSessionFactory", sqlSessionTemplateRef = "turboBootCrawlerSqlSessionTemplate")
public class TurboBootCrawlerDataSourceAutoConfiguration extends TurboDataSourceAutoConfiguration {


    @Resource
    private TurboBootCrawlerDatasourceProperties turboBootCrawlerDatasourceProperties;


    @Bean
    public DataSource turboBootCrawlerDataSource() {
        return buildDruidDataSource(turboBootCrawlerDatasourceProperties);
    }


    @Bean
    public SqlSessionFactory turboBootCrawlerSqlSessionFactory(@Qualifier("turboBootCrawlerDataSource") DataSource dataSource) throws Exception {
        return buildSqlSessionFactory(dataSource, turboBootCrawlerDatasourceProperties);
    }


    @Bean
    public SqlSessionTemplate turboBootCrawlerSqlSessionTemplate(@Qualifier("turboBootCrawlerSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    public DataSourceTransactionManager turboBootCrawlerDataSourceTransactionManager(@Qualifier("turboBootCrawlerDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
